package com.ruoyi.web.controller.app;

import com.ruoyi.business.domain.TWithdrawApply;
import com.ruoyi.business.service.ITWithdrawApplyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * B端 提现接口
 */
@RestController
@RequestMapping("/b-api/withdraw")
public class BapiWithdrawController extends BaseController {

    @Autowired
    private ITWithdrawApplyService withdrawApplyService; // 代码生成器已创建

    /**
     * 获取提现记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TWithdrawApply withdrawApply) {
        // 强制设置当前用户ID，确保数据隔离
        withdrawApply.setUserId(SecurityUtils.getUserId());

        startPage();
        List<TWithdrawApply> list = withdrawApplyService.selectTWithdrawApplyList(withdrawApply);
        return getDataTable(list);
    }

    /**
     * 申请提现
     */
    @PostMapping("/apply")
    public AjaxResult apply(@RequestBody TWithdrawApply withdrawApply) {
        // TODO: 在Service层实现申请提现的完整逻辑
        // 1. 校验提现金额是否大于0
        // 2. 校验银行卡信息是否完整
        // 3. 校验可提现余额是否足够
        // 4. 扣减用户可提现余额 (需要事务)
        // 5. 插入提现申请记录

        // 这里是简化版实现
        withdrawApply.setUserId(SecurityUtils.getUserId());
        withdrawApply.setUserNickname(SecurityUtils.getLoginUser().getUser().getNickName());
        withdrawApply.setStatus("0"); // 待审核
        withdrawApply.setCreateBy(SecurityUtils.getUsername());
        return toAjax(withdrawApplyService.insertTWithdrawApply(withdrawApply));
    }
}